package cn.com.infcn.monitor.task.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.mina.core.session.IoSession;
import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.infcn.monitor.bean.IFCFileSystem;
import cn.com.infcn.monitor.bean.IFCResponseBean;
import cn.com.infcn.monitor.util.IFCBeanUtils;
import cn.com.infcn.monitor.util.IFCMinitorConstanst;

/**
 * 获取文件系统信息
 * 
 * @author jjs
 *
 */
public class FileSystemTask extends BaseTask {

	private final static Logger logger = LoggerFactory
			.getLogger(FileSystemTask.class);

	public FileSystemTask(IoSession session, Object message) {
		super(session, message);
	}

	@Override
	public void execute() throws Exception {
		IFCResponseBean result = fileSystem();
		sendResponse(result);
	}

	private IFCResponseBean fileSystem() throws SigarException {
		IFCResponseBean result = new IFCResponseBean();

		try{
			Sigar sigar = new Sigar();
			FileSystem fslist[] = sigar.getFileSystemList();
			List<IFCFileSystem> list = new ArrayList<IFCFileSystem>();
			for (int i = 0; i < fslist.length; i++) {
	
				FileSystem fs = fslist[i];
				switch (fs.getType()) {
				case 0: // TYPE_UNKNOWN ：未知
					break;
				case 1: // TYPE_NONE
					break;
				case 2: // TYPE_LOCAL_DISK : 本地硬盘
					FileSystemUsage usage = sigar.getFileSystemUsage(fs
							.getDirName());
	
					IFCFileSystem fileSystem = new IFCFileSystem();
					IFCBeanUtils.copyProperties(fs, fileSystem);
					IFCBeanUtils.copyProperties(usage, fileSystem);
					list.add(fileSystem);
					break;
				case 3:// TYPE_NETWORK ：网络
					break;
				case 4:// TYPE_RAM_DISK ：闪存
					break;
				case 5:// TYPE_CDROM ：光驱
					break;
				case 6:// TYPE_SWAP ：页面交换
					break;
				}
			}
			IFCFileSystem[] fsArray = new IFCFileSystem[list.size()];
			list.toArray(fsArray);
			result.setResult(fsArray);
			result.setResponseCode(IFCMinitorConstanst.MONITOR_RESPONSE_CODE_OK);
		} catch (Exception e) {
			logger.error(e.getMessage());
			result.setResponseCode(IFCMinitorConstanst.MONITOR_RESPONSE_CODE_ERROR);
			result.setResponseMsg(e.getMessage());
		}
		return result;
	}

}
